package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.web.service.accept.util.ConnectProdUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @ClassName : CommunicationPlanSwitchingHandler
 * @Description : 通信计划切换
 * @Author : li.fangfang
 * @Date : 2021/12/08 11:32
 */
@Service
public class CommunicationPlanSwitchingHandler extends AbstractAttrHandler {
    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContextNew().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        List<StaticAttrDO> attrValuesTx = Lists.newArrayList();

        // 融合场景
        if (InstType.GIFT_OFFER_INST.name().equals(sceneInst.getRootInstType().name())) {
            // 取所有接入类产品
            List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
            for (int i = 0; i < kiteProdInstList.size(); i++) {
                KiteProdInst prodInst = kiteProdInstList.get(i);
                KiteAttr kiteAttr = prodInst.getAttr("200003687");
                if (Objects.nonNull(kiteAttr)) {
                    // 通用类产品
                    StaticAttrDO staticAttrDO = new StaticAttrDO();
                    staticAttrDO.setAttrValueDesc(kiteAttr.getValue());
                    staticAttrDO.setAttrValue(kiteAttr.getValue());
                    attrValuesTx.add(staticAttrDO);
                    break;
                }
                // 定制类通过功能产品找
                List<KiteProdInstFunc> kiteProdInstFuncs = querySceneInstService.listProdInstFuncs(sceneInst.getCustId(), prodInst.getProdInstId());
                if (KiteListUtils.isNotEmpty(kiteProdInstFuncs)) {
                    kiteProdInstFuncs.stream().forEach(kiteProdInstFunc -> {
                        if (KiteStringUtils.equalsAny(ProductCache.get(kiteProdInstFunc.getSpecId()).getProdNbr(), LocalKeyConsts.FUNC_PROD_ID_TX)) {
                            KiteAttr prodInstFuncAttr = kiteProdInstFunc.getAttr("200003687");
                            if (Objects.nonNull(prodInstFuncAttr)) {
                                // 通用类产品
                                StaticAttrDO staticAttrDO = new StaticAttrDO();
                                staticAttrDO.setAttrValueDesc(prodInstFuncAttr.getValue());
                                staticAttrDO.setAttrValue(prodInstFuncAttr.getValue());
                                attrValuesTx.add(staticAttrDO);
                            }
                        }
                    });
                }
            }
            if (KiteListUtils.isNotEmpty(attrValuesTx)) {
                commonAttr.setAttrValues(attrValuesTx);
            }
        }
        else {
            // 非融合场景
            String business_number = querySceneInstService.getCommonAttrValue(sceneInstId, "business_number");
            Map<String, Object> map = ConnectProdUtil.getPlanNanmesByAccNum(sceneInst.getCustId(), business_number);
            List<String> communicationPlanNbrs = (List<String>) map.get("tx_plan");

            communicationPlanNbrs.forEach(number -> {
                StaticAttrDO staticAttrDO = new StaticAttrDO();
                staticAttrDO.setAttrValueDesc(number);
                staticAttrDO.setAttrValue(number);
                attrValuesTx.add(staticAttrDO);
            });
            commonAttr.setAttrValues(attrValuesTx);
        }
    }

}
